package com.yiren.manager.ctrl.beds;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.StringUtil;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrBed;
import com.yiren.entity.YrRoom;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.beds.YrBedForm;
import com.yiren.manager.form.beds.YrRoomForm;
import com.yiren.manager.service.iface.beds.YrBedService;
import com.yiren.manager.service.iface.beds.YrRoomService;
import com.yiren.manager.utils.ObjectUtils;

/**
 * 床位控制类
 * @author Administrator
 *
 */
@RequestMapping("/bed")
@Controller
public class YrBedController extends GenericController<YrBedForm, YrBed>{
	

	@Autowired
	private YrBedService yrBedService;
	
	@Autowired
	private YrRoomService yrRoomService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrBedForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrBedForm> result = new HTTPResult<YrBedForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrBedForm form = tool.mapper("bed", YrBedForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getBedId()||form.getBedId().isEmpty()){
			form.setBedId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrBed entity = ObjectUtils.objectToObject(form, YrBed.class);
		yrBedService.save(entity);
		
		return result;
	}
	
	/**获取所有记录*/
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrBedForm> findAll() throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrBedForm> result = new HTTPResult<YrBedForm>();
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("bedNumber", "ASC");
		QueryResult<YrBed> qr = yrBedService.find(YrBed.class, null, new Object[]{},map);
		List<YrBed> entityList = qr.getResultList();
		List<YrBedForm> formList = new ArrayList<YrBedForm>();
		for(YrBed bed : entityList){
			YrBedForm form = ObjectUtils.objectToObject(bed, YrBedForm.class);
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	/**删除一条记录*/
	@RequestMapping("delete")
	@ResponseBody
	public HTTPResult<YrBedForm> delete(String id) throws MyException{
		HTTPResult<YrBedForm> result = new HTTPResult<YrBedForm>();
		if(StringUtil.isEmpty(id)){
			throwExp("param.error");
		}
		yrBedService.delete(id);
		return result;
	}
	
	/**获取一条记录*/
	@RequestMapping("findOne")
	@ResponseBody
	public HTTPResult<YrBedForm> findOne(String id) throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrBedForm> result = new HTTPResult<YrBedForm>();
		if(null==id){
			throwExp("param.error");
		}
		YrBed entity = yrBedService.findOne(id);
		List<YrBedForm> formList = new ArrayList<YrBedForm>();
		formList.add(ObjectUtils.objectToObject(entity, YrBedForm.class));
		result.setData(formList);
		return result;
	}

	/**
	 * 房间id获取床位列表
	 * @param buildingId
	 * @param floorsId
	 * @param roomId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("findByRoomId")
	@ResponseBody
	public HTTPResult<YrBedForm> findByRoomId(String roomId) throws Exception{
		HTTPResult<YrBedForm> result = new HTTPResult<YrBedForm>();
		if(roomId==null){
			throwExp("param.error");
		}
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("bedNumber", "ASC");
		String sqlWhere = "room_id=?1";
		QueryResult<YrBed> qr = yrBedService.find(YrBed.class, sqlWhere, new Object[]{roomId},map);
		
		List<YrBedForm> formList = new ArrayList<YrBedForm>();
		for(YrBed bed : qr.getResultList()){
			formList.add(toForm(bed));
		}
		result.setData(formList);
		return result;
	}
	
	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrBedForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrBedForm> result = new HTTPResult<YrBedForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for(String id : ids){
			yrBedService.delete(id);
		}
		return result;
	}
	
	/**
	 * 
	 * @param floorId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("findByFloorId")
	@ResponseBody
	public HTTPResult<YrRoomForm> findByFloorId(String floorId) throws Exception{
		HTTPResult<YrRoomForm> result = new HTTPResult<YrRoomForm>();
		String sqlWhere = "floor_id=?1";
		QueryResult<YrRoom> qrRoom = yrRoomService.find(YrRoom.class, sqlWhere, new Object[]{floorId});
		
		List<YrRoomForm> formList = new ArrayList<YrRoomForm>();
		
	
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("bedNumber", "ASC");
		for(YrRoom room: qrRoom.getResultList()){
			sqlWhere = "room_id = ?1";
			QueryResult<YrBed> qrBed = yrBedService.find(YrBed.class, sqlWhere, new Object[]{room.getRoomId()},map);
			List<YrBedForm> bedForms = new ArrayList<YrBedForm>();
			for(YrBed bed : qrBed.getResultList()){
				bedForms.add(ObjectUtils.objectToObject(bed, YrBedForm.class));
			}
			YrRoomForm roomForm = ObjectUtils.objectToObject(room, YrRoomForm.class);
			roomForm.setBedList(bedForms);
			formList.add(roomForm);
		}
		
		result.setData(formList);
		return result;
	}
	
	@Override
	public YrBedForm toForm(YrBed e) throws Exception {
		YrBedForm form = ObjectUtils.objectToObject(e, YrBedForm.class);
		return form;
	}

	@Override
	public YrBed toEntity(YrBedForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
